﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloOne.Store.EF
{
    public class Repository<T> : RepositoryBase<T> where T : class, IEntity
    {
        public Repository(DbContext db)
        {
            this.db = db;
        }

        public override IList<T> GetEntity(Func<T, bool> func)
        {
            var set = this.db.Set<T>();

            //强制从数据库中读取
            var entits = set.ToList();

            return entits.Where(func).ToList();
        }

        private DbContext db;

        public override IList<T> GetEntityFast(Func<T, bool> func)
        {
            var set = this.db.Set<T>();

            return set.Where(func).ToList();
        }

        public override void Save(T entity)
        {
            if (Find(entity.ID) == null)
                this.db.Set<T>().Add(entity);
            var result = this.db.SaveChanges();
        }
    }
}